<?php
/*******************************************************************************
 * 一些链接
 *******************************************************************************/
if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
	$vars['linkmore'] = array(
		'sales'		=> PUB_DIR.'news/sales/',
		'salesrank'	=> PUB_DIR.'goods/search.do?orderby_sales=desc',
	);
}else{
	$vars['linkmore'] = array(
		'sales'		=> WEB_DIR.'index.php?mdl=news&mark=sales',
		'salesrank'	=> WEB_DIR.'index.php?mdl=goods&orderby_sales=desc',
	);
}

if(empty($_GET['mark'])){
	$_GET['catid'] = empty($_GET['catid']) ? 0 : intval($_GET['catid']);
}else{
	$_GET['catid'] = Goodscat::get_category_by_mark($_GET['mark'], 'id');
}
if($_GET['catid'] > 0){
	$__category_id = $_GET['catid'];
	$childs = Goodscat::get_child_categorys($__category_id);
	if(!empty($childs)){
		//unset($_GET['catid']);
		$_GET['childs'] = implode(',', $childs);
	}
	$_category_info = Goodscat::get_category($_GET['catid']);

	/*******************************************************************************
	 * 品牌
	 *******************************************************************************/
	$brand_arr = Goodscat::get_category($_GET['catid'], 'brand', true);
	if(!empty($brand_arr)){
		$_GET['brand'] = empty($_GET['brand']) ? array() : $_GET['brand'];
		if(!is_array($_GET['brand'])){
			$_GET['brand'] = array($_GET['brand']);
		}
		$brand_ids = explode(',', trim($brand_arr, ', '));
		$oBrand = new Brand();
		$brand_list = array();
		foreach($brand_ids as $brandid){
			$brand_list[$brandid] = $oBrand->get_row($brandid, '`id`,`name`,`logo`');
			if(in_array($brandid, $_GET['brand'])){
				$brand_list[$brandid]['checked'] = 1;
			}
		}
		$vars['brand_list'] = $brand_list;
	}

	/*******************************************************************************
	 * 价格区间
	 *******************************************************************************/
	$range_list = Goodscat::get_category($_GET['catid'], 'range', true);
	$_GET['range'] = empty($_GET['range']) ? '' : trim($_GET['range']);
	foreach($range_list as $key => $row){
		$row['item'] = "{$row[0]}-{$row[1]}";
		if($row['item'] == $_GET['range']){
			$row['checked'] = 1;
		}
		$range_list[$key] = $row;
	}
	$vars['range_list'] = $range_list;
	
	/*******************************************************************************
	 * 规格
	 *******************************************************************************/
	$spec_arr = Goodscat::get_category($_GET['catid'], 'spec', true);
	if(!empty($spec_arr)){
		$_GET['prototype'] = empty($_GET['prototype']) ? array() : $_GET['prototype'];
		$prototypes = Prototype::get_prototypes();
		$spec_list = array();
		foreach ($spec_arr as $prototypeid => $row) {
			$spec = $prototypes[$prototypeid];
			if($spec['type'] == 1){
				$spec['multi'] = $row['multi'];
				$spec['empty'] = $row['empty'];
				foreach($spec['value'] as $key => $value){
					if(!in_array($key, $row['item'])){
						unset($spec['value'][$key]);
						continue;
					}
					$checked = 0;
					if(array_key_exists($prototypeid, $_GET['prototype'])){
						if(is_array($_GET['prototype'][$prototypeid]) && in_array($key, $_GET['prototype'][$prototypeid])){
							$checked = 1;
						}elseif($key == $_GET['prototype'][$prototypeid]){
							$checked = 1;
						}
					}
					$spec['value'][$key] = array('value' => $value, 'checked' => $checked);					
				}
				$spec_list[$prototypeid] = $spec;
			}
		}
		$vars['spec_list'] = $spec_list;
	}
}

// 商品列表
$__pagesize = 32;
$oGoods = new Goods();
$goods_list = $oGoods->get_list('*', $_GET, $_GET, $__page, $__pagesize);
foreach($goods_list['list'] as $key => $goods){
	$goods_query = empty($goods_query) ? '' : '?'.implode('&', $goods_query);
	if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
		if(empty($goods['filename'])){
			$goods['url'] = PUB_DIR.'goods/'.time_to_path($goods['addtime']).'/'.$goods['id'].'.html';
		}else{
			$goods['url'] = PUB_DIR.'goods/'.$goods['filename'].'.html';
		}
	}else{
		$goods['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$goods['id']}";
	}
	$goods_list['list'][$key] = $goods;
}
$vars['goods_list'] = $goods_list['list'];
$vars['pagelist'] = $goods_list['pagelist'];


// 广告
$_f_advert = cache_read('adverts.php');
$_f_adpos = cache_read('adposs.php');
foreach($_f_advert['pos_ads_mark']['indexSecondCoupon'] as $adverid => $advert_info){
	$advert_info['pos'] = $_f_adpos['list_id'][$advert_info['posid']];
	if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
		$advert_info['url'] = PUB_DIR."ads/{$advert_info['id']}.html";
	}else{
		$advert_info['url'] = WEB_DIR."index.php?mdl=ads&do=view&id={$advert_info['id']}";
	}
	$_f_advert['pos_ads_mark']['indexSecondCoupon'][$adverid] = $advert_info;
}
$vars['indexSecondCoupon'] = array_shift($_f_advert['pos_ads_mark']['indexSecondCoupon']);

/*******************************************************************************
 * 销售排行榜
 *******************************************************************************/
$goodslist_salesrank = $oGoods->get_list('`id`,`name`,`price`,`sales`,`label`,`labeltype`,`cover`,`filename`,`addtime`', array('kept' => " AND `sales`>0", 'isdel' => 0), array('orderby_sales' => 'DESC', 'orderby_views' => 'DESC', 'orderby_id' => 'DESC'), 1, 5, 0);
foreach($goodslist_salesrank['list'] as $key => $goods){
	if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
		if(empty($goods['filename'])){
			$goods['url'] = PUB_DIR.'goods/'.time_to_path($goods['addtime']).'/'.$goods['id'].'.html';
		}else{
			$goods['url'] = PUB_DIR.'goods/'.$goods['filename'].'.html';
		}
	}else{
		$goods['url'] = WEB_DIR."index.php?mdl=goods&do=view&id={$goods['id']}";
	}
	$goodslist_salesrank['list'][$key] = $goods;
}
$vars['goodslist_salesrank'] = $goodslist_salesrank['list'];


/*******************************************************************************
 * 页面信息　& 当前位置
 *******************************************************************************/
if($__f_sysvars['url_rewrite'] == 'html' || $__f_sysvars['url_rewrite'] == 'rewrite'){
	if(empty($_category_info)){
		url_here('全部分类', PUB_DIR."goods/");
	}else{
		url_here($_category_info['name'], PUB_DIR."goods/{$_category_info['mark']}/");
	}
}else{
	if(empty($_category_info)){
		url_here('全部分类', WEB_DIR."index.php?mdl=goods");
	}else{
		url_here($_category_info['name'], WEB_DIR."index.php?mdl=goods&mark={$_category_info['mark']}");
	}
}
?>